//
//  MTRedpacketCellRow.swift
//  MT
//
//  Created by ooops on 2020/5/15.
//  Copyright © 2020 张强. All rights reserved.
//

import Foundation
import Eureka
import UIKit
import MTCategory


public final class MTRedpacketCellRow: OptionsRow<PushSelectorCell<Dictionary<String, String>>>, PresenterRowType, RowType {
    
    public typealias PresenterRow = MTCreateClubRedpacketController
    
    /// Defines how the view controller will be presented, pushed, etc.
    public var presentationMode: PresentationMode<PresenterRow>?
    
    /// Will be called before the presentation occurs.
    public var onPresentCallback: ((FormViewController, PresenterRow) -> Void)?
    
    /// 显示模式
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "名称"
        l.textAlignment = .right
        l.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        return l
    }()
    
    lazy var contentLabel: UILabel = {
        let txf = UILabel()
        txf.textColor = UIColor.text.x9b9b9b
        txf.font = PingFangSCMediumFont(14)
        txf.textAlignment = .right
        return txf
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        return v
    }()
    
    lazy var customAccessoryView: UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "icon_right_gary"))
        return imageV
    }()

    public required init(tag: String?) {
        super.init(tag: tag)
        cell.textLabel?.isHidden = true
        cell.accessoryView?.isHidden = true
        cell.accessoryType = .none
        cell.backgroundColor = UIColor.theme.background
        cell.selectionStyle = .none
        
        cell.contentView.addSubview(tipLabel)
        cell.contentView.addSubview(lineView)
        cell.contentView.addSubview(customAccessoryView)
        cell.contentView.addSubview(contentLabel)
        
        tipLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.width.greaterThanOrEqualTo(30)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(tipLabel.snp.right)
            make.right.equalTo(customAccessoryView.snp.left).offset(-9)
        }

        
        customAccessoryView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-8)
            make.centerY.equalTo(tipLabel)
            make.width.height.equalTo(15)
        }
        
        let v = MTCreateClubRedpacketController(){ _ in }
        if let va = self.value {
            v.row.value = va
        }
        
        presentationMode = .show(controllerProvider: ControllerProvider.callback { return v }, onDismiss: { vc in _ = vc.navigationController?.popViewController(animated: true) })

        displayValueFor = {
            guard let location = $0 else { return "" }
            if let s = location.keys.first, s.count > 0 {
                self.contentLabel.text = "已设置"
                return ""
            }
            self.contentLabel.text = "未设置"
            return ""
        }
    }
    
    public override func updateCell() {
        super.updateCell()
        tipLabel.text = self.title
        if let s = self.value?.keys.first, s.count > 0 {
            self.contentLabel.text = "已设置"
        } else {
            self.contentLabel.text = "未设置"
        }
    }
    
    public override func customUpdateCell() {
        super.customUpdateCell()
        cell.accessoryType = .none
    }
    
    /**
     Extends `didSelect` method
     */
    public override func customDidSelect() {
        super.customDidSelect()
        guard let presentationMode = presentationMode, !isDisabled else { return }
        if let controller = presentationMode.makeController() {
            controller.row = self
            controller.row.value = self.value
            controller.title = selectorTitle ?? controller.title
            onPresentCallback?(cell.formViewController()!, controller)
            presentationMode.present(controller, row: self, presentingController: self.cell.formViewController()!)
        } else {
            presentationMode.present(nil, row: self, presentingController: self.cell.formViewController()!)
        }
    }
    
    /**
     Prepares the pushed row setting its title and completion callback.
     */
    public override func prepare(for segue: UIStoryboardSegue) {
        super.prepare(for: segue)
        guard let rowVC = segue.destination as? PresenterRow else { return }
        rowVC.title = selectorTitle ?? rowVC.title
        rowVC.onDismissCallback = presentationMode?.onDismissCallback ?? rowVC.onDismissCallback
        onPresentCallback?(cell.formViewController()!, rowVC)
        rowVC.row = self
    }
}



